//
//  ResumeEditViewController.m
//  eshow
//
//  Created by Hisoka on 16/1/13.
//  Copyright © 2016年 Hisoka. All rights reserved.
//

#import "ResumeEditViewController.h"
#import "WebViewJavascriptBridge.h"
#import "ResumePreViewController.h"

@interface ResumeEditViewController ()<UIActionSheetDelegate, UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) WebViewJavascriptBridge *bridge;
@property (nonatomic, strong) UIBarButtonItem *saveBTN;

@end

@implementation ResumeEditViewController

- (void) viewWillAppear:(BOOL)animated {
//    [_webView reload];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customizeBackItem];
//    __weak typeof (self) weakSelf = self;
//    [self customizeBackItemWithCallBack:^{
//        [_bridge send:@"saveDraft" responseCallback:^(id responseData) {
//            NSLog(@"js response is %@", responseData);
//            [weakSelf.navigationController popViewControllerAnimated:YES];
//        }];
//    }];
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setStyle];

}

- (void) setStyle {

    self.navigationItem.title = _navTitle;
    
    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    
    UIBarButtonItem *publishBTN = [[UIBarButtonItem alloc] initWithTitle:@"发布"
                                                                   style:UIBarButtonItemStyleDone
                                                                  target:self
                                                                  action:@selector(reqPublishResume)];
    
    if ([_resumeType isEqualToString:@"draft"]) {
//        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"更多"
//                                                                                  style:UIBarButtonItemStylePlain
//                                                                                 target:self
//                                                                                 action:@selector(showSheetAction)];
        _saveBTN = [[UIBarButtonItem alloc] initWithTitle:@"保存"
                                                                    style:UIBarButtonItemStyleDone
                                                                   target:self
                                                                   action:@selector(reqSaveDraft)];
    }else{
//        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存简历"
//                                                                                  style:UIBarButtonItemStyleDone
//                                                                                 target:self
//                                                                                 action:@selector(reqSaveResume)];
        _saveBTN = [[UIBarButtonItem alloc] initWithTitle:@"保存"
                                                                    style:UIBarButtonItemStyleDone
                                                                   target:self
                                                                   action:@selector(reqSaveResume)];

    }
    
    self.navigationItem.rightBarButtonItems = @[_saveBTN, publishBTN];

    self.navigationItem.rightBarButtonItem.tintColor = ORANGE_COLOR;
    
    self.webView = [[UIWebView alloc] initForAutoLayout];
    [self.view addSubview:_webView];
    
    [_webView autoPinToTopLayoutGuideOfViewController:self withInset:0];
    [_webView autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.view];
    [_webView autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.view];
    [_webView autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.view];
    _webView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    
    _webView.delegate = self;
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_resumeUrl]];
    
    [_webView loadRequest:request];
    
    self.bridge = [WebViewJavascriptBridge bridgeForWebView:_webView handler:^(id data, WVJBResponseCallback responseCallback) {
        responseCallback(@"objc => Right back atcha");
    }];
    
    [SVProgressHUD show];
    [self performSelector:@selector(dismissSVHUD) withObject:nil afterDelay:1.5f];
    
}

- (void) dismissSVHUD {
    [SVProgressHUD dismiss];
}

- (void) showSheetAction {
    NSArray *actionNameArr = @[@"生成简历", @"保存为草稿"];
    UIActionSheet *saveAS = [[UIActionSheet alloc] initWithTitle:nil
                                              delegate:self
                                     cancelButtonTitle:@"取消"
                                destructiveButtonTitle:nil
                                     otherButtonTitles:nil];
    [actionNameArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [saveAS addButtonWithTitle:obj];
    }];
    
    [saveAS showInView:self.view];
}

- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) return;
    
    if (buttonIndex == 1) {
        [self reqSaveResume];
    }else{
        [self reqSaveDraft];
    }
}

#pragma mark - 保存草稿
- (void) reqSaveDraft {
    [_bridge send:@"saveDraft" responseCallback:^(id responseData) {
//        NSLog(@"js response is %@", responseData);
    }];
}

#pragma mark - 保存简历
- (void) reqSaveResume {
    [MobClick event:@"Click_MakeResume_editBTN"];

    [_bridge send:@"saveForm" responseCallback:^(id responseData) {
//        NSLog(@"js response is %@", responseData);
    }];
}

#pragma mark - 发布简历
- (void) reqPublishResume {
    [MobClick event:@"Click_MakeResume_publishBTN"];

    [_bridge send:@"saveForm" responseCallback:^(id responseData) {
//        NSLog(@"js response is %@", responseData);
        if ([[responseData objectForKey:@"status"] isEqualToString:@"0"]) {
            ResumePreViewController *resumePreVC = [[ResumePreViewController alloc] init];
            resumePreVC.preViewUrl = [[responseData  objectForKey:@"data"] objectForKey:@"url"];
            resumePreVC.shareContent = [[responseData objectForKey:@"data"] objectForKey:@"text"];
            resumePreVC.rid = [[responseData objectForKey:@"data"] objectForKey:@"rid"];
            resumePreVC.tid = self.tid;
            resumePreVC.vcName = @"myResumeVC";
            [self.navigationController pushViewController:resumePreVC animated:YES];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
